﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using StockholmsStad.Windows.UI.Modules.Minister.Models;
using StockholmsStad.Windows.UI.Modules.Minister.Interfaces;
using StockholmsStad.Runtime.Serialization.DataContract;
using System.Collections.Generic;
using System.Windows.Documents;
using StockholmsStad.Utilities;
using System.Diagnostics;

namespace StockholmsStad.Windows.UI.Modules.Minister.Controls
{
    /// <summary>
    /// Interaction logic for MinisterDetailsControl.xaml
    /// </summary>
    public partial class MinisterDetailsControl
    {
        private readonly IMinisterService ministerService;
        private MinisterPresentationModel model;
        private readonly List<ValueIdPair> languages;
        private const string NO_LANGUAGE = "Inget språk angivet";

        public MinisterDetailsControl(IMinisterService service)
        {
            ministerService = service;
            languages = ministerService.GetLanguages();
            Initialized += MinisterDetailsControl_Initialized;
            InitializeComponent();

            DataContextChanged += MinisterDetailsControl_DataContextChanged;
        }

        private void MinisterDetailsControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            model = DataContext as MinisterPresentationModel;

            if(model != null)
                model.PropertyChanged += model_PropertyChanged;
        }

        private void model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (model != null)
                CancelButton.IsEnabled = !CancelButton.IsEnabled;
        }

        private void MinisterDetailsControl_Initialized(object sender, EventArgs e)
        {
            cbLanguages.ItemsSource = languages;
        }

        private void btnLanguage_Click(object sender, RoutedEventArgs e)
        {
            if(cbLanguages.SelectedValue == null)
            {
                MessageBox.Show("Du måste välja ett språk i listan", "Information", MessageBoxButton.OK,
                                MessageBoxImage.Information);
                return;
            }

            var selectedLang = (ValueIdPair)cbLanguages.SelectedItem;

            if(lbLanguages.Items.Count == 1 && lbLanguages.Items[0].ToString().Equals(NO_LANGUAGE))
                lbLanguages.Items.Clear();


            if(lbLanguages.Items.Contains(selectedLang.Name))
            {
                MessageBox.Show("Språket finns redan i listan", "Information", MessageBoxButton.OK,
                                MessageBoxImage.Information);
                return;
            }

            AddLanguage(selectedLang.Name);
            cbLanguages.SelectedIndex = -1;
        }

        private void btnOtherLanguage_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(tbLanguage.Text))
            {
                MessageBox.Show("Du måste välja ett språk i listan", "Information", MessageBoxButton.OK,
                                MessageBoxImage.Information);
                return;
            }

            if (lbLanguages.Items.Contains(tbLanguage.Text))
            {
                MessageBox.Show("Språket finns redan i listan", "Information", MessageBoxButton.OK,
                                MessageBoxImage.Information);
                return;
            }

            AddLanguage(tbLanguage.Text);
            tbLanguage.Text = string.Empty;
        }

        private void AddLanguage(string language)
        {
            if (model != null)
            {
                if (model.Languages == null)
                    model.Languages = new ObservableCollection<string>();

                model.Languages.Add(language);
                lbLanguages.Items.Refresh();
            }
        }

        private void DeleteLanguage(string language)
        {
            if (model != null && model.Languages != null)
            {
                model.Languages.Remove(language);
                lbLanguages.Items.Refresh();
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            CancelButton.IsEnabled = !CancelButton.IsEnabled;
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            //SaveButton.IsEnabled = !SaveButton.IsEnabled;
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            var link = new Hyperlink {TargetName = "_blank"};

            string strUri = SettingsUtility.GetAppSetting(Facade.Properties.Resources.ReportViewer);

            if (RadioButtonPrintMinisterService.IsChecked.HasValue && RadioButtonPrintMinisterService.IsChecked.Value)
            {
                strUri = strUri +
                         SettingsUtility.GetAppSetting(Facade.Properties.Resources.MinisterServiceReportPath) +
                         "&" + SettingsUtility.GetAppSetting(Facade.Properties.Resources.UriCommand) +
                         "&" + SettingsUtility.GetAppSetting(Facade.Properties.Resources.UriParameters) +
                         "&ministerId=" + model.MinisterId;

                link.NavigateUri = new Uri(strUri);

                //link.NavigateUri = new Uri("http://parvis.input.nu/ReportServer/Pages/ReportViewer.aspx?/Reports/MinisterService&rs:Command=Render&rc:Parameters=false&ministerId=1");
            }
            if (RadioButtonPrintReimbursement.IsChecked.HasValue && RadioButtonPrintReimbursement.IsChecked.Value)
            {
                if (dpDateFrom.SelectedDate.HasValue &&
                    dpDateTo.SelectedDate.HasValue)
                {
                    var fromDate = ((DateTime) dpDateFrom.SelectedDate).ToShortDateString();
                    var toDate = ((DateTime)dpDateTo.SelectedDate).ToShortDateString();
                    strUri = strUri +
                             SettingsUtility.GetAppSetting(Facade.Properties.Resources.ReimbursementReportPath) +
                             "&" + SettingsUtility.GetAppSetting(Facade.Properties.Resources.UriCommand) +
                             "&" + SettingsUtility.GetAppSetting(Facade.Properties.Resources.UriParameters) +
                             "&fromDate=" + fromDate +
                             "&toDate=" + toDate +
                             "&ministerId=" + model.MinisterId;

                    link.NavigateUri = new Uri(strUri);

                    //link.NavigateUri = new Uri("http://parvis.input.nu/ReportServer/Pages/ReportViewer.aspx?/Reports/Reimbursement&rs:Command=Render&rc:Parameters=false&ministerId=1&fromDate=2009-01-01");
                }
                else
                {
                    MessageBox.Show("Du måste ange ett datum!", "", MessageBoxButton.OK, MessageBoxImage.Error);
                }

            }
            
            if(link.NavigateUri != null)
            {
                Process.Start(link.NavigateUri.AbsoluteUri);
            }
        }

        private void btnDeleteLanguage_Click(object sender, RoutedEventArgs e)
        {
            if (lbLanguages.SelectedValue == null)
            {
                MessageBox.Show("Du måste välja ett språk i listan", "Information", MessageBoxButton.OK,
                                MessageBoxImage.Information);
                return;
            }

            DeleteLanguage(lbLanguages.SelectedItem.ToString());
            lbLanguages.SelectedIndex = -1;
        }

        private void lbLanguages_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {

        }
    }
}
